import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import styleImport from 'vite-plugin-style-import'
import viteCompression from 'vite-plugin-compression'
import viteImagemin from 'vite-plugin-imagemin'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    // 开启ref语法糖
    vue({
      refTransform: true
    }),
    viteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false
      },
      optipng: {
        optimizationLevel: 7
      },
      webp: {
        quality: 75
      },
      mozjpeg: {
        quality: 65
      },
      pngquant: {
        quality: [0.65, 0.9],
        speed: 4
      },
      svgo: {
        plugins: [
          {
            removeViewBox: false
          },
          {
            removeEmptyAttrs: false
          }
        ]
      }
    }),
    // css样式按需加载
    styleImport({
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: (name) => `vant/es/${name}/style`
        }
        // {
        //   libraryName: 'element-plus',
        //   esModule: true,
        //   ensureStyleFile: true,
        //   resolveStyle: name => {
        //     if (name === 'locale') return ''
        //     return `element-plus/lib/theme-chalk/${name}.css`
        //   },
        //   resolveComponent: name => {
        //     return `element-plus/lib/${name}`
        //   }
        // }
      ]
    }),
    // 开启gzip或brotli来压缩资源
    viteCompression({
      // 开启gzip模式
      verbose: true,
      disable: false,
      deleteOriginFile: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    })
  ],
  base: './', // 打包路径
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src') // 设置别名
    }
  },
  /** 指定多个 .html 文件作为入口点*/
  build: {
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, 'index.html')
      }
    },
    brotliSize: false, // => 启用/禁用brotli压缩大小报告
    // 去除console
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "./src/style/index.scss";` // 你的scss文件路径
      }
    }
  },
  // server: {
  //   port: 8090, // 启动端口
  //   // host: 'wx.yuegongjia.com',
  //   open: true,
  //   proxy: {
  //     '/soms': {
  //       target: 'http://cs.gongyoushijie.com',
  //       changeOrigin: true,
  //       rewrite: (path) => path.replace(/^\/soms /, '/soms')
  //     },
  //   },
  //   cors: true
  // }
})
